//
//  StatusOriginalView.swift
//  ydy_weibo
//
//  Created by 杨德勇 on 16/7/18.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

import UIKit
import SnapKit


class StatusOriginalView: UIView {
     //原创微博底部约束  用来记录底部约束  后续根据是否有配图 来动态修改
    var bottomConstraints: Constraint?
    
    
    //MARK:-
    //MARK:4.把数据源属性加上
    var status:Status?{
        didSet{
            //把数据源赋值到控件上
            iconImage.sd_setImageWithURL(status?.user?.headImageURL,placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = status?.user?.name
            memberIconView.image = status?.user?.mbrankImage
            verifiedIconView.image = status?.user?.verified_type_Image
            timelabel.text = status?.creat_at ?? "12:12"
            sourcelabel.text = status?.source ?? "隔壁的隔壁"
            contentLabel.text = status?.text
            
            //移除底部约束给数据源
            self.bottomConstraints?.uninstall()
            
            if let picurls = status?.imageURLs where picurls.count > 0{
                
                pictureView.imageURLs = status?.imageURLs
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomConstraints = make.bottom.equalTo(contentLabel.snp_bottomMargin).offset(StatusCellMargin).constraint
                })
            }else{
                //来这里说明这条微博没图片
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomConstraints = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            
            }
            
        }
    }

    
    
    //MARK:-
    //MARK:3.重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    //MARK:-
    //MARK:2.setupUI
    private func setupUI(){
        sepView.backgroundColor = UIColor(white: 0.9, alpha: 1)
        addSubview(sepView)
        addSubview(iconImage)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(verifiedIconView)
        addSubview(timelabel)
        addSubview(sourcelabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        
        //设置约束
        sepView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
            
        }
        
        iconImage.snp_makeConstraints { (make) in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
            make.width.equalTo(StatusIconWidth)
            make.height.equalTo(StatusIconWidth)
            
        }
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconImage.snp_top)
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
            
        }
        memberIconView.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(nameLabel.snp_top)
        }
        
        verifiedIconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconImage.snp_right)
            make.centerY.equalTo(iconImage.snp_bottom)
            
        }
        
        timelabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImage.snp_bottom)
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
        }
        
        
       sourcelabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timelabel.snp_bottom)
            make.left.equalTo(timelabel.snp_right).offset(StatusCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconImage)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
           
        }
        //原创微博的底部相对于微博正文的底部对齐
        self.snp_makeConstraints { (make) -> Void in
            //把图片底部约束好之后，把它记录下来
            self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }

        
    }

    
    //MARK:-
    //MARK:1.懒加载所有控件
    
    // cell之间的分割线
    private lazy var sepView:UIView  = UIView()
    //头像
    private lazy var iconImage:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLabel :UILabel = UILabel(title: "用户名", fontSize: 14, color: UIColor.darkGrayColor())
    //用户认证标志
    private lazy var verifiedIconView :UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //用户等级
    private lazy var memberIconView :UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    //发布时间
    private lazy var timelabel = UILabel(title: "2016.1.1", fontSize: 14, color: UIColor.lightGrayColor())
    //微博来源
    private lazy var sourcelabel = UILabel(title: "来源用户", fontSize: 14, color: UIColor.lightGrayColor())

    //微博内容（文字）
    private lazy var contentLabel = UILabel(title: "内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容", fontSize: 14, color: UIColor.lightGrayColor(), margin: StatusCellMargin)
    private lazy var pictureView: StatusPictureView = StatusPictureView()
}

